var YOUXISOSO_WEB_ROOT = "http://www.youxisoso.com/";

document.write("<link type='text/css'  media='all' href='" + 
		YOUXISOSO_WEB_ROOT + 
		"wp-content/themes/youxisoso/searchcomponent/css/search.css' rel='stylesheet' />");

document.write("<link type='text/css' href='" + YOUXISOSO_WEB_ROOT + 
		"wp-content/themes/youxisoso/searchcomponent/css/jquery.autocomplete.css' rel='stylesheet' />");

document.write("<script type='text/javascript' src='" + YOUXISOSO_WEB_ROOT + 
		"wp-content/themes/youxisoso/searchcomponent/jquery.js'></script>");

document.write("<script type='text/javascript' src='" + YOUXISOSO_WEB_ROOT +
		"wp-content/themes/youxisoso/searchcomponent/jquery.autocomplete.js'></script>");


//组件配置参数
var youxisosoSearchInputParamer = {
	inputDivId:null,
	resultDivId:null,
	resultURL:null,
	cruGame:null,
	formID:'youxisososearchform',
	gamePrix:'gameli',
	cat:[
		{id:"",name:"全部游戏"},
		{id:"6",name:"Flash游戏"},
		{id:"7",name:"网页游戏"},
		{id:"8",name:"手机游戏"} 
	]
}; 
youxisosoSearchInputParamer.cruGame = youxisosoSearchInputParamer.cat[0];

/******************************************************
	js1：搜索框组建
******************************************************/

/**
 * 监听是否查询
 */
function goSearch(event){
	if(event.keyCode==13) {
		getResult(null,null,0) ;
	}
}

/**
 * 异步获取服务数据
 * @param getkeyword
 * @param getcat
 * @param nowpage
 * @param fixdiv
 * @return
 */
function getResult(getkeyword,getcat,nowpage,fixdiv){
	
	if(nowpage == undefined){
		nowpage = 0 ;
	}
	if(getkeyword== null || getkeyword == undefined){
		if($('#all')!=null){
			getkeyword = $('#all').val();
		}else{
			getkeyword = "" ;
		}
	}
	if(getcat== null || getcat == undefined){
		if("" == youxisosoSearchInputParamer.cruGame.id ){
			getcat = "" ;
		}else{
			getcat = youxisosoSearchInputParamer.cruGame.name ;
		}
	}
	
	if(fixdiv !=null && "" != fixdiv){
		youxisosoSearchInputParamer.resultDivId = fixdiv ;
	}
	if(youxisosoSearchInputParamer.resultURL != undefined
			&& youxisosoSearchInputParamer.resultURL != null 
			&&youxisosoSearchInputParamer.resultURL != ""){
		$('#resultCAT').val(getcat) ;	
		$('#resultDIV').val(youxisosoSearchInputParamer.resultDivId) ;
		if(youxisosoSearchInputParamer.resultURL.indexOf("?")>=0){
			document.getElementById(youxisosoSearchInputParamer.formID).action 
				= youxisosoSearchInputParamer.resultURL+"&keyword="+getkeyword+"&cat="+getcat+"&div="+youxisosoSearchInputParamer.resultDivId;
		}else{
			document.getElementById(youxisosoSearchInputParamer.formID).action
				= youxisosoSearchInputParamer.resultURL+"?keyword="+getkeyword+"&cat="+getcat+"&div="+youxisosoSearchInputParamer.resultDivId;
		}
		$("#"+youxisosoSearchInputParamer.formID).submit() ;
		return ;
	}
	var data = {
		'paged':nowpage,
		'keywords':getkeyword,
		'cat':getcat,
		'fixdiv':youxisosoSearchInputParamer.resultDivId 
	};

	var url = YOUXISOSO_WEB_ROOT+"?page_id=183012&callback=?";
	$.getJSON(
		url,
		data,
		function(msg) {
			var html = "" ;
			html = msg.html ;
			var htmldiv = msg.fixdiv ;
			if(youxisosoSearchInputParamer.resultURL == undefined ){
				$("#" + youxisosoSearchInputParamer.resultDivId).empty(); 
				$("#" + youxisosoSearchInputParamer.resultDivId).append(html) ;
			}
//			else{
//				//表单提交
//				$("#resultCAT").val(html); ;
//				$("#resultDIV").val(htmldiv);
//				$("#"+youxisosoSearchInputParamer.formId).submit() ;
//			}
		} 
	);

}

/**
 * 联想数据加载
 * @return
 */
function init_search_keyword(){
	var cat = youxisosoSearchInputParamer.cruGame ;
	$("#all").autocomplete(YOUXISOSO_WEB_ROOT+"?page_id=176565&cat="+cat,
		{my_url:YOUXISOSO_WEB_ROOT+"?page_id=176565&cat="+cat.id+"&keyword="});
}

//改变游戏类型
function fireLiChange(index){
	for(var i=0 ; i <youxisosoSearchInputParamer.cat.length;i++){
		if(index==youxisosoSearchInputParamer.gamePrix + youxisosoSearchInputParamer.cat[i].id){
			youxisosoSearchInputParamer.cruGame = youxisosoSearchInputParamer.cat[i] ;
			break ;
		}
	}
	initSearchInput(youxisosoSearchInputParamer.inputDivId,
			youxisosoSearchInputParamer.resultDivId,
			youxisosoSearchInputParamer.resultURL);
}

/**
 * 初始化搜索组件
 * @param inputDivId
 * @param resultDivId
 * @param resultURL
 * @return
 */
function initSearchInput(inputDivId,resultDivId,resultURL){
	//保留配置参数
	youxisosoSearchInputParamer.inputDivId = inputDivId ;
	youxisosoSearchInputParamer.resultDivId = resultDivId ;
	youxisosoSearchInputParamer.resultURL = resultURL;
	
	if($("#" + inputDivId)!= null){
		var html = "<div id='list_bottom'><div id='soso'><ul>" ;
		for(var i = 0 ; i < youxisosoSearchInputParamer.cat.length ;i++){
			html +=	"<li id='" + youxisosoSearchInputParamer.gamePrix + youxisosoSearchInputParamer.cat[i].id+"'"+
					" class='menuNo'><a  title='"+youxisosoSearchInputParamer.cat[i].name+"'"+
					" onclick='fireLiChange(\"" + youxisosoSearchInputParamer.gamePrix + youxisosoSearchInputParamer.cat[i].id+"\");' >"+
					youxisosoSearchInputParamer.cat[i].name +"</a></li>" ;
		}
		html += " </ul></div>";
		html += "	<div id='sousuo1' class='sousuo'>" ;
		if(resultURL !=null&& resultURL!=undefined){
			html+="		<form action='"+resultURL+"' method='post' id='"+youxisosoSearchInputParamer.formID+"'> ";
		}
		html += "<input type='hidden' id='resultCAT' name='cat'>" ;	
		html += "<input type='hidden' id='resultDIV' name='div'>" ;	
		html += "<ul>"+
			"				<li class='soso_li'>"+
			"        			<input type='text' onkeyup='goSearch(event);' " +
			"						onfocus='init_search_keyword();' id='all' name='keyword' class='soso_text ac_input' autocomplete='off'>"+
			"        		</li> "+
			"        		<li><input type='button' id='sousuoclick' onclick='getResult(null,null,null);' class='soso_button' value='搜索'></li>"+
			"        		<li><a href='#'>"+
			"      				<img alt='随便玩玩' id='list_pindaoplay' " +
			"						src='http://www.youxisoso.com/wp-content/themes/youxisoso/images/suibianwanwan.gif'></a>"+
			"      			</li>"+
			"      		</ul>" ;
		if(resultURL !=null && resultURL!=undefined){
			html+="      	</form>"  ;
		}
		html += " </div></div>" ;
		
		$("#" + inputDivId).empty(); 
		$("#" + inputDivId).append(html) ;
		$("#" + youxisosoSearchInputParamer.gamePrix+youxisosoSearchInputParamer.cruGame.id).removeClass('menuNo').addClass('menuOn') ;
	}
	
}